#include "Mystdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>

#define FILE_MODE 0666


//"w","a","r"
//int open(const char *pathname, int flags, ...
//                  /* mode_t mode */ );

_FILE * _fopen(const char*filename, const char *flag)
{   
    _FILE *fp = (_FILE*)malloc(sizeof(_FILE));
    if(fp==NULL)
        return NULL;
    assert(filename);
    assert(flag);

    int f = 0;
    int fd =-1;
    if(strcmp("w",flag)==0)
    {
        f=(O_CREAT|O_WRONLY|O_TRUNC);
        fd = open(filename,f,FILE_MODE);

    }
    else if(strcmp("a",flag)==0)
    {
        f=(O_CREAT|O_WRONLY|O_APPEND);
        fd = open(filename,f,FILE_MODE);
    }
    else if(strcmp(flag, "r") == 0) 
    {
        f = O_RDONLY;
        fd = open(filename, f,FILE_MODE);
    }
    else
    {
        return NULL;
    }

    if(fd ==-1)
    {
        return NULL;
    }
    fp->flag = FLUSH_ALL;
    fp->out_pos = 0;
    fp->fileno = fd;
    return fp;
}                                                           

int _fwrite(_FILE *fp, const char *s, int len)
{
    // "abcd\n"
    memcpy(&fp->outbuffer[fp->out_pos], s, len);
    fp->out_pos += len;

    if(fp->flag&FLUSH_NOW)
    {
        write(fp->fileno, fp->outbuffer, fp->out_pos);
        fp->out_pos = 0;
    }
    else if(fp->flag&FLUSH_LINE)
    {
        if(fp->outbuffer[fp->out_pos-1] == '\n'){ // 不考虑其他情况
            write(fp->fileno, fp->outbuffer, fp->out_pos);
            fp->out_pos = 0;
        }
    }
    else if(fp->flag & FLUSH_ALL)
    {
        if(fp->out_pos == SIZE){
            write(fp->fileno, fp->outbuffer, fp->out_pos);
            fp->out_pos = 0;
        }
    }

    return len;
}

void _fflush(_FILE *fp)
{
    if(fp->out_pos > 0){
        write(fp->fileno, fp->outbuffer, fp->out_pos);
        fp->out_pos = 0;
    }
}


void _fclose(_FILE *fp)
{
    if(fp==NULL)
    {
        return NULL;
    }
    _fflush(fp);
    close(fp->fileno);
    free(fp);
}
